//
//  LKAddShopTreasureViewController.m
//  Mideleague_Shop
//
//  Created by Zhiwang on 2020/7/10.
//  Copyright © 2020 Zhiwang. All rights reserved.
//

#import "LKAddShopTreasureViewController.h"
#import "LKLockScanViewController.h"
@interface LKAddShopTreasureViewController ()
@property(nonatomic,strong)UFFormView *formView;
@property(nonatomic,strong)UITextField *noteTf;
@end

@implementation LKAddShopTreasureViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"添加商家宝";
//    [self setNavgationRightBtn];
    
    [self createView];
    // Do any additional setup after loading the view.
}
-(void)createView{
    UIView *headerView = [[UIView alloc] init];
    headerView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:headerView];
    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.view.mas_top).offset(NAVIGATION_BAR_HEIGHT + 10);
        make.height.mas_equalTo(52);
    }];
    LKBaseLabel *topLabel = [LKBaseLabel labelWithFrame:CGRectZero text:@"商家宝" textColor:COLOR_HEX(0x333333) font:[UIFont fontWithName:@"PingFangSC-Regular" size:16] textAlignment:NSTextAlignmentLeft];
    [headerView addSubview:topLabel];
    [topLabel mas_makeConstraints:^(MASConstraintMaker *make) {
      make.left.equalTo(headerView).offset(20);
       make.centerY.equalTo(headerView);
    }];
    
    UIView *tfView = [self createFormViewWithNote:@"请输入商家宝后12位编号"];
    [self.view addSubview:tfView];
    [tfView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(headerView.mas_bottom).offset(1);
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(52);
    }];
}
-(void)setNavgationRightBtn{
    WS(weakSelf)
    LKBaseButton *rightBtn = [LKBaseButton buttonWithFrame:CGRectMake(0, 0, 64, 24) type:UIButtonTypeCustom title:@"使用帮助" titleColor:COLOR_HEX(0x333333) layercorne:13 font:[UIFont fontWithName:@"PingFangSC-Regular" size: 16] backgroudColor:[UIColor clearColor] AndActionBlock:^(UIButton * _Nonnull btn) {

    }];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
}

/// 创建备注view
-(UIView *)createFormViewWithNote:(NSString *)note{
    WS(weakSelf)
    UIView *bgView = [[UIView alloc] init];
    bgView.backgroundColor = [UIColor whiteColor];
    LKBaseButton *scanBtn = [LKBaseButton buttonWithFrame:CGRectMake(0, 0, 64, 24) type:UIButtonTypeCustom title:@"" titleColor:COLOR_HEX(0x333333) layercorne:13 font:[UIFont fontWithName:@"PingFangSC-Regular" size: 16] backgroudColor:[UIColor clearColor] AndActionBlock:^(UIButton * _Nonnull btn) {
        [weakSelf chooseErweima];
    }];
    [scanBtn setImage:[UIImage imageNamed:@"device_scan"] forState:UIControlStateNormal];
    [bgView addSubview:scanBtn];
    [scanBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgView).offset(20);
        make.centerY.equalTo(bgView);
        make.size.mas_equalTo(CGSizeMake(30, 30));
    }];
    LKBaseButton *bindBtn = [LKBaseButton buttonWithFrame:CGRectMake(0, 0, 64, 24) type:UIButtonTypeCustom title:@"绑定" titleColor:[UIColor whiteColor] layercorne:5 font:[UIFont fontWithName:@"PingFangSC-Regular" size: 12] backgroudColor:yellowDefaultColor AndActionBlock:^(UIButton * _Nonnull btn) {
        if ([NSString isBlankString:weakSelf.noteTf.text]) {
            [SVProgressHUD showInfoWithStatus:@"请输入商家宝编号"];
            return ;
        }
        NSMutableDictionary *param = [[NSMutableDictionary alloc] initWithDictionary:@{@"type":@"3",@"sn":weakSelf.noteTf.text}];
        NSString *sign = [NSString encoingWithDic:param];
        [param setObject:sign forKey:@"sign"];
        [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerlUrl apiPath:MyShopTreasureAddUrl header:[LKUserInforTool account].token parameters:param progress:^(NSProgress * _Nullable progress) {
            
        } success:^(BOOL isSuccess, id  _Nullable responseObject) {
            if ([responseObject[@"code"] isEqualToString:@"success"]) {
                [SVProgressHUD showInfoWithStatus:@"添加成功"];
                [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadShopTreasureListData" object:nil];
                [weakSelf.navigationController popViewControllerAnimated:YES];
            }else{
                [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
            }
        } failure:^(NSString * _Nullable errorMessage) {
            
        }];
    }];
    [bgView addSubview:bindBtn];
    [bindBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(bgView).offset(-20);
        make.size.mas_equalTo(CGSizeMake(80, 20));
        make.centerY.equalTo(bgView);
    }];
    self.noteTf = [[UITextField alloc] init];
    self.noteTf.placeholder = note;
    self.noteTf.textAlignment = NSTextAlignmentLeft;
    [bgView addSubview:self.noteTf];
    [self.noteTf mas_makeConstraints:^(MASConstraintMaker *make) {
       make.right.equalTo(bindBtn.mas_left).offset(-20);
       make.left.equalTo(scanBtn.mas_right).offset(20);
        
       make.centerY.equalTo(bgView);
    }];
    return bgView;
}
-(void)chooseErweima{
    WS(weakSelf)
    LBXScanViewStyle *style = [[LBXScanViewStyle alloc]init];
    style.centerUpOffset = 44;
    style.photoframeAngleStyle = LBXScanViewPhotoframeAngleStyle_Inner;
    style.photoframeLineW = 3;
    style.photoframeAngleW = 18;
    style.photoframeAngleH = 18;
    style.isNeedShowRetangle = NO;
    style.anmiationStyle = LBXScanViewAnimationStyle_LineMove;
    //qq里面的线条图片
    UIImage *imgLine = [UIImage imageNamed:@"qrcode_scan_light_green"];
    style.animationImage = imgLine;
    style.notRecoginitonArea = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
    LKLockScanViewController *vc = [[LKLockScanViewController alloc] init];
    vc.shopTreasureBlock = ^(NSString *codeStr) {
        weakSelf.noteTf.text = codeStr;
    };
    vc.style = style;
    vc.isOpenInterestRect = YES;
    vc.libraryType = SLT_Native;
    vc.scanCodeType = SCT_BarCode128;
    vc.scanType = LKScanTypeHardware;
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
